package com.bbcare.cp.plat.service.impl;

import java.util.Calendar;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bbcare.comm.Constants;
import com.bbcare.comm.SeqUtil;
import com.bbcare.cp.plat.entity.cp.IaOperationRecord;
import com.bbcare.cp.plat.service.IOprRecordService;
import com.bbcare.cp.plat.store.ibatis.cp.IOperationRecordDAO;

/**
 * @name: 操作日志记录接口实现
 * @author: DemonLee
 * @createTime: 2018.5.7
 * @description: 操作日志记录，通用接口
 * @modify:
 * 
 */
@Service
public class OprRecordServiceImpl implements IOprRecordService {
    private final Log logger = LogFactory.getLog(OprRecordServiceImpl.class);

    @Autowired
    public IOperationRecordDAO operationRecordDao;

    /**
     * @name: 记录操作日志接口实现
     * @author: DemonLee
     * @createTime: 2018.5.7
     * @param: 日志记录
     * @return:
     * @modify:
     * @description:
     */
    @Override
    @Transactional
    public void saveOprRecordService(Map<String, Object> mapIn) throws Exception {
        logger.debug("in saveOprRecordService mapIn==" + mapIn);
        IaOperationRecord oprRecord = (IaOperationRecord) mapIn.get("OperationRecord");
        operationRecordDao.saveOperationRecord(oprRecord);

        return;
    }
    
    /**
     * @name: 记录操作日志接口实现
     * @author: wuyz
     * @createTime: 2018.5.11
     * @param: 日志记录
     * @return:
     * @modify:
     * @description:
     */
    @Override
    @Transactional
    public void saveOprRecordServiceHandle(Map<String, Object> mapIn) throws Exception {
        logger.debug("in saveOprRecordServiceHandle mapIn==" + mapIn);
        //获取日历
        Calendar now = Calendar.getInstance();
        //获取月份
	    String month = String.valueOf(now.get(Calendar.MONTH) + 1);
	    //获取天数
	    String day = String.valueOf(now.get(Calendar.DAY_OF_MONTH));
	    
        IaOperationRecord OperationRecord = new IaOperationRecord();
	    OperationRecord.setBreifIntro(null);
	    OperationRecord.setBusiCategory(null);
	    OperationRecord.setBusiId((String) mapIn.get("BusiId"));
	    OperationRecord.setChannelId((String) mapIn.get("sourceId"));
	    OperationRecord.setChannelType((String) mapIn.get("sourceType"));
	    OperationRecord.setDay(day);
	    OperationRecord.setId(SeqUtil.getSeqNo(Constants.SEQ_NO_FOR_SYSTEMID));
	    OperationRecord.setMonth(month);
	    OperationRecord.setOpCode((String) mapIn.get("opCode"));
	    OperationRecord.setOpDesc((String) mapIn.get("opNote"));
	    OperationRecord.setOpTime((String) mapIn.get("updateTime"));
	    if (StringUtils.isBlank((String) mapIn.get("opType"))) {
	    	OperationRecord.setOpType((String) mapIn.get("opType"));
	    } else {
	    	OperationRecord.setOpType(Constants.PATIENT_ACTIVE_BUSI_TYPE_6);
	    }
	    OperationRecord.setOpUser((String) mapIn.get("authorId"));
	    if (Constants.OP_TYPE_SELF.equals((String) mapIn.get("opType"))) {
	        OperationRecord.setOpUsertype(Constants.OPCODE_TYPE_PATIENT);
	    } else {
	        OperationRecord.setOpUsertype(Constants.OPCODE_TYPE_DOCTOR);
	    }
	    OperationRecord.setProjectRoleId((String) mapIn.get("projectRoleId"));
	    OperationRecord.setQueueId((String) mapIn.get("queueId"));
	    OperationRecord.setRemark((String)mapIn.get("remarkDesc"));
	    OperationRecord.setTaskId((String)mapIn.get("TaskId"));
	    OperationRecord.setTenantId((String) mapIn.get("tenantId"));
	    OperationRecord.setUpdateAccept(SeqUtil.getSeqNo());
	    OperationRecord.setUserId((String) mapIn.get("userId"));
        operationRecordDao.saveOperationRecord(OperationRecord);
        return;
    }

}
